﻿using System;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Collections.Generic;

using HsTec.Mobile.Green.BLL;
using Baimasoft.Manage.SqlServerDAL;

public partial class ReportAnalysis_ForecastBusinessBenchMark : System.Web.UI.Page
{
    ForecastBusinessBLL bll = new ForecastBusinessBLL();
    AreaCategoryBLL ac = new AreaCategoryBLL();
    Param MyP = new Param();

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((string)Session["Baimasoft_Pers_ID"] == null)
        {
            Response.Redirect("../Logon.aspx");
        }
        if (!this.IsPostBack)
        {
            try
            {
                Indicator.Text = Request.QueryString["sIndicator"].ToString();
            }
            catch
            {

            }
            GridView1.Attributes.Add("BorderColor", "LightSkyBlue");
            this.BindsltArea();
            this.txtYearsDataBind();
        }
    }
    private void BindGridViewData()
    {
        if (txtYears.SelectedValue.Trim() == "")
        {
            Response.Write("<script language=javascript>alert('请先选择年月后设置地市标杆值！');</script>");
            return;
        }
        GridView1.DataSource = bll.GetBenchMarkList(GetSearchString());
        GridView1.DataBind();
        HiddenID.Value = "";
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            HtmlSelect sltType = (HtmlSelect)(e.Row.Cells[5].FindControl("sltType"));
            sltType.DataTextField = "Name";
            sltType.DataValueField = "ID";
            sltType.DataSource = ac.GetSltList(txtYears.SelectedValue.Trim());
            sltType.DataBind();
            sltType.Items.Insert(0, "请选择...");
            sltType.Items[0].Value = "";
                        
            Label lblValue = (Label)(e.Row.Cells[8].FindControl("lblValue"));
            lblValue.Text = e.Row.Cells[9].Text;

            sltType.Value = e.Row.Cells[7].Text + "|" + e.Row.Cells[9].Text;
            sltType.Attributes.Add("onchange", "sltChange(this.value," + lblValue.ClientID + ");");
        }
    }
    private string GetSearchString()
    {
        string searchStr = "(1=1)";

        string sddlRegionid = sltArea.SelectedValue.ToString().Trim();
        if (sddlRegionid != "")
        {
            searchStr += " and Area_ID  ='" + sddlRegionid + "' ";
        }
        if (txtYears.SelectedValue.Trim() != "")
        {
            searchStr += " and Years  ='" + txtYears.SelectedValue.Trim() + "' ";
        }
        return searchStr;
    }
    protected void linkBtnGo_Click(object sender, EventArgs e)
    {

    }
    private void BindsltArea()
    {
        sltArea.DataTextField = "Name";
        sltArea.DataValueField = "ID";
        sltArea.DataSource = bll.GetAreaList();
        sltArea.DataBind();
        sltArea.Items.Insert(0, "请选择县市区域...");
        sltArea.Items[0].Value = "";
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        bll.GetList(GetSearchString()).Sort = e.SortExpression;
        this.BindGridViewData();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.BindGridViewData();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindGridViewData();
    }
    private void txtYearsDataBind()
    {
        txtYears.DataTextField = "Name";
        txtYears.DataValueField = "Name";
        txtYears.DataSource = MyP.GetCodeByParentCode("1");
        txtYears.DataBind();
        txtYears.Items.Insert(0, "请选择...");
        txtYears.Items[0].Value = "";
    }
    protected void btnAdd_ServerClick(object sender, EventArgs e)
    {
        string[] ary = new string[2];
        Dictionary<string, string> dc = new Dictionary<string, string>();
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            HtmlSelect sltType = (HtmlSelect)(GridView1.Rows[i].Cells[6].FindControl("sltType"));
            ary = sltType.Value.Split('|');
            dc.Add(GridView1.Rows[i].Cells[0].Text.ToString(), ary[0]);
        }
        int iBack = bll.SetBenchMark(dc);
        if (iBack == 1)
        {
            Response.Write("<script language=javascript>alert('修改成功！');</script>");
        }
        else
        {
            Response.Write("<script language=javascript>alert('修改失败！');</script>");
        }
        BindGridViewData();
    }
}
